﻿using System;
using System.Drawing;
using System.Windows.Forms;
using HHControls.CreditEducation.Properties;

namespace HHControls.CreditEducation
{
    public class HHArrowExpand : Panel
    {
        #region Fields

        private Bitmap _currentImage;
        private bool _isExpand;
        private Timer _timerHover;

        #endregion

        #region Constructors

        public HHArrowExpand()
        {
            InitializeTimer();
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            //this.Bounds = new Rectangle(Width / 10, Width / 5, (Width * 4) / 5, Height - Width / 5);
            DoubleBuffered = true;
            Width = 190;
            Height = 100;
            ExpandedSize = Height;
            IsExpand = true;
            Font = new Font("Times New Roman", (float)Width / 12, FontStyle.Bold, GraphicsUnit.Point);
            ForeColor = Color.White;
        }

        private void InitializeButton()
        {
            ImageNormal = new Bitmap(Resources.button_hover, Width, Width / 5);
            ImageHover = new Bitmap(Resources.button_normal, Width, Width / 5);
            ImageClicked = new Bitmap(Resources.button_clicked, Width, Width / 5);
            _currentImage = ImageNormal;
        }

        #endregion

        #region Properties

        public Bitmap ImageNormal { get; set; }
        public Bitmap ImageHover { get; set; }
        public Bitmap ImageClicked { get; set; }
        public int ExpandedSize { get; set; }
        public string TextTitle { get; set; }
        public bool IsExpand
        {
            get { return _isExpand; }
            set
            {
                _isExpand = value;
                
                if (_isExpand)
                {
                    _timerHover.Enabled = true;
                }
                else
                {
                    Height = _isExpand ? ExpandedSize : Width / 5;
                }
            }
        }

        #endregion

        #region Override Methods

        protected override void OnSizeChanged(EventArgs e)
        {
            InitializeButton();
            Invalidate();
            base.OnSizeChanged(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawImage(e.Graphics);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            _currentImage = ImageClicked;
            Invalidate();
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            _currentImage = ImageHover;
            Invalidate();
            base.OnMouseUp(e);
        }

        protected override void OnMouseHover(EventArgs e)
        {
            _currentImage = ImageHover;
            Invalidate();
            base.OnMouseHover(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            _currentImage = ImageNormal;
            Invalidate();
            base.OnMouseLeave(e);
        }

        #endregion

        #region Paint

        private void DrawImage(Graphics graphics)
        {
            graphics.DrawImage(_currentImage, 0, 0);

            var layoutRectangle = new Rectangle(0, 0, Width, Width / 5);
            var stringFormat = new StringFormat
                                   {
                                       Alignment = StringAlignment.Center,
                                       LineAlignment = StringAlignment.Center
                                   };
            graphics.DrawString(TextTitle, Font, new SolidBrush(ForeColor), layoutRectangle, stringFormat);
        }

        #endregion

        #region Timer

        private void InitializeTimer()
        {
            _timerHover = new Timer { Interval = 1 };

            _timerHover.Tick += (s, e) =>
                                    {
                                        if (_isExpand)
                                        {
                                            Height += 4;
                                        }
                                        else
                                        {
                                            Height -= 4;
                                        }

                                        if (Height >= Width / 5 && Height <= ExpandedSize) return;

                                        _timerHover.Enabled = false;
                                        Height = _isExpand ? ExpandedSize : Width / 5;
                                    };
        }

        #endregion
    }
}